﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenGL;

namespace OSM
{
	public partial class Form1 : GLForm
	{
		OSMData D = new OSMData();
		List<double[]> Borders = new List<double[]>();
		List<int> Levels = new List<int>();

		public Form1()
			: base()
		{
			//D.ImportOSM("C:\\OSMData\\hungary.osm");
			//D.Save();
			D.Load(OSMCore.OSMPath + "admin\\");
			//D.Save("C:\\OSMData\\x\\");

			MessageBox.Show("Loaded\n"
				+ D.NodeID.Count
				+ " Nodes\n"
				+ D.WayID.Count
				+ " Lines\n" + D.RelID.Count + " Relations"
			);
			//D.Save();

			Borders.Clear();
			Levels.Clear();

			V3G Center = V3G.Interpolate(new V3G(D.BoundMinX, D.BoundMinY), new V3G(D.BoundMaxX, D.BoundMaxY), 0.5);
			Center = new V3G(19.0404707, 47.4983815);
			int L = -1;
			foreach (OSMRelation R in D.RelID.Values)
				if (R["boundary"] == "administrative" && int.TryParse(R["admin_level"], out L))
				{
					Poly3G Brd = null;//R.MakeBorder();
					if (Brd == null)
					{
						Console.WriteLine(R);
					}
					else
					{
						Levels.Add(L);
						List<double> x = new List<double>();
						/*for (int i = 0; i < Brd.Points.Count-1; i++)
						{
							V3D P = Center.GetLocalDirection(new V3G(Brd.Points[i].Longitude, Brd.Points[i].Latitude)) * 0.0002;
							x.Add(P.X);
							x.Add(0);
							x.Add(-P.Y);
						}*/

						Borders.Add(x.ToArray());
					}
				}
		}
		protected override void InitGL()
		{
		}
		V3D Cam = new V3D(5, 5, 5);

		List<int> Idx = new List<int>();
		List<double> F = new List<double>();
		Random R = new Random();
		protected override void ProcessGLKey(Keys K, bool shift, bool control)
		{
			if (K == Keys.Tab)
			{
				Idx.Add(R.Next(Borders.Count));
				F.Add(0);
			}
		}

		protected override void Process(double dt, double tt)
		{
			Cam.DoRot(V3D.Green, dt * 5);
			Cam = new V3D(0, 10, 20);

			for (int i = 0; i < Idx.Count;)
			{
				if (F[i] >= 1)
				{
					Idx.RemoveAt(i);
					F.RemoveAt(i);
				}
				else
				{
					F[i] += dt / 2;
					i++;
				}
			}
		}
		protected override bool DrawGLScene()
		{
			MakePerspective(90, 0.1f, 10000000);
			GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT);
			GL.gluLook3D(Cam, V3D.Blue*15, V3D.Green);
			GL.glCol3D(V3D.White);
			GL.glEnableClientState(GL.GL_VERTEX_ARRAY);
			GL.glLineWidth(3);

			V3D[] C = { V3D.Black, V3D.Black, V3D.Red, V3D.Black, V3D.Black, V3D.Green, V3D.Blue, V3D.Black };
			int[] LW = { 0, 0, 5, 3, 3, 1, 1, 1 };

				GL.glDisable(GL.GL_DEPTH_TEST);
			for (int i = 0; i < Borders.Count; i++)
			{
				GL.glLineWidth(1);
				GL.glVertexPointer3d(0, Borders[i]);
				if (Idx.Contains(i))
				{
					GL.glEnable(GL.GL_STENCIL_TEST);

					GL.glStencilFunc(GL.GL_ALWAYS, 1, 0xFFFF);
					GL.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_INCR);
					GL.glColorMask(0, 0, 0, 0);
					GL.glDrawArrays(GL.GL_TRIANGLE_FAN, 0, Borders[i].Length / 3);
					
					GL.glColorMask(1, 1, 1, 1);
					GL.glStencilFunc(GL.GL_EQUAL, 1, 1);
					GL.glCol3D(V3D.Red);
					GL.glDisable(GL.GL_STENCIL_TEST);
					GL.glDrawArrays(GL.GL_TRIANGLE_FAN, 0, Borders[i].Length / 3);
				}
				GL.glCol3D(V3D.White);
				GL.glDrawArrays(GL.GL_LINE_LOOP, 0, Borders[i].Length / 3);
				GL.glLineWidth(1);
			}
				GL.glEnable(GL.GL_DEPTH_TEST);
			GL.glDisableClientState(GL.GL_VERTEX_ARRAY);
			//GL.glBegin(GL.GL_LINES);
			//GL.DrawSign();
			return true;
		}
	}
}